﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System.Windows.Media.Animation;
using MapHello.Service;
using MapHello.Data;

namespace MapHello.Control
{
    public partial class NavigationBarControl : UserControl
    {
        public event EventHandler<string> NavigationItemTappedEvent;

        public NavigationBarControl()
        {
            InitializeComponent();

            this.Loaded += OnLoaded;
        }

        public bool IsExpand { get; set; }

        void OnLoaded(object sender, RoutedEventArgs e)
        {
            MapHelloService service = new MapHelloService();
            var config = service.LoadConfig(Config.MyNavigationPages);
            var values = config.Value.Split('|');
            navigationItems.ItemsSource = values.Where<string>(i => i != this.CurrentKey).ToList<string>();
        }

        private string _displayName;
        public string DisplayName
        {
            get
            {
                return _displayName;
            }
            private set
            {
                _displayName = value;
                this.currentName.Text = value;
            }
        }

        private string _currentKey;
        public string CurrentKey
        {
            get
            {
                return _currentKey;
            }
            set
            {
                _currentKey = value;
                this.DisplayName = value;
            }
        }

        private void OnExpandHideButtonTapped(object sender, System.Windows.Input.GestureEventArgs e)
        {
            var widthAnimation = hideStory.Children.FirstOrDefault<Timeline>(i => i.GetType() == typeof(DoubleAnimation)) as DoubleAnimation;
            if (!IsExpand)
            {
                expandStory.Begin();
                IsExpand = true;
            }
            else
            {
                hideStory.Begin();
                IsExpand = false;
            }
        }

        private void OnNavigationButtonTapped(object sender, System.Windows.Input.GestureEventArgs e)
        {
            if (null != this.NavigationItemTappedEvent)
            {
                NavigationItemTappedEvent(sender, (sender as Button).CommandParameter.ToString());
            }
        }
    }
}
